package com.example.Film.reviews.website.controllers;

import com.example.Film.reviews.website.dao.MovieRepository;
import com.example.Film.reviews.website.dao.PerformerRepository;
import com.example.Film.reviews.website.entities.Movie;
import com.example.Film.reviews.website.entities.Performer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("performers")
public class PerformerController {

    @Autowired
    PerformerRepository performerRepository;

    @Autowired
    MovieRepository movieRepository;

    @GetMapping
    public String displayPerformer(Model model){
       List<Performer> performers = performerRepository.findAll();
       model.addAttribute("performers",performers);
//        List<Movie> movies = movieRepository.findAll();
//        model.addAttribute("allMovies",movies);
        return "performer/list-performer";
    }
    @GetMapping("/new")
    public String displayPerformerForm(Model model){

//        List<Movie> movies= movieRepository.findAll();
//        model.addAttribute("allMovies",movies);

        Performer aPerformer = new Performer();
        model.addAttribute("performer",aPerformer);
        return "performer/new-performer";
    }
    @PostMapping("/save")
    public String createPerformer(Model model, Performer performer){
        performerRepository.save(performer);
        return "redirect:/performers";
    }
}
